﻿
namespace MoneyManagerNET.Presentation.ViewModel
{
    using Model;
    using MoneyManagerNET.Presentation.Infrastructure;
    using MoneyManagerNET.Presentation.ViewModel.Base;


    public class ContragentListViewModel:ListViewModel<IContragent>//ViewModelBase
    {
        public ContragentListViewModel()
            : base(App.Storage.Select<IContragent>())
        {
            this.NewCommand = new DelegateCommand((o)=>New());
            this.EditCommand = new DelegateCommand((o) => Edit());
        }

        string _text;
        public string Text { 
            get { return _text; }
            set { _text = value; this.OnChange(() => Text); } 
        }

        protected override void Moved()
        {
            base.Moved();
            Text=Selected==null?string.Empty:Selected.Name;

            RefreshCommandStatuses();
        }

        protected override void RefreshCommandStatuses()
        {
            base.RefreshCommandStatuses();
            EditCommand.RaiseCanExecuteChanged();
            NewCommand.RaiseCanExecuteChanged();
        }


        public DelegateCommand NewCommand { get; protected set; }    
        public void New() {
            var c=App.Storage.New<IContragent>();
            c.Name = Text; 
            Items.Add(c);

            RefreshCommandStatuses();
        }

        public DelegateCommand EditCommand { get; protected set; }
        public void Edit()
        {
            var contragent = base.Selected;

            if (contragent != null)
            {
                contragent.Name = this.Text;
                RaisePropertyChanged(contragent,"Name");
            }
        }


        
    }
}
